package com.forum.service;

import com.forum.dao.LabelDao;
import com.forum.jspmine.common.util.IdWorker;
import com.forum.pojo.Label;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author:jayChou
 * @date:2019/7/19 description:神来之手
 * 动态获取菜单
 */

@Service
public class LabelService {
    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker  idWorker;
    /**
    *@author:jayChou
    *@date:2019/9/10
    *description:查询全部
    *
    */
    public List<Label> findAll(){

        Random random = new Random();

        int i = random.nextInt(100);
        if (i<50){
            throw new RuntimeException("出异常了");
        }

        return labelDao.findAll();
    }

    /**
    *@author:jayChou
    *@date:2019/9/10
    *description:查询一个
    *
    */


    public Label findById(String id){
        return labelDao.findById(id).get();
    }


    public void add(Label label){
        long nextId = idWorker.nextId();
        label.setId(String.valueOf(nextId));
        labelDao.save(label);

    }



    public void delete(String id){

        labelDao.deleteById(id);
    }


    public void update(Label label){
          labelDao.save(label);

    }

    public List<Label> search(Label label) {
        //动态查询
        Specification<Label> specification = predicate(label);
        return labelDao.findAll(specification);




    }

    public Page<Label> search(Integer page, Integer size, Label label) {
        Specification<Label> specification = predicate(label);
        Page pageLable = labelDao.findAll(specification, PageRequest.of(page - 1, size));
        return pageLable;


    }


    private  Specification<Label>  predicate(Label label){
        Specification<Label> spec = new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();

                if(StringUtils.isNotEmpty(label.getLabelname())){
                    Predicate labelName = criteriaBuilder.like(root.get("labelname"), "%" + label.getLabelname() + "%");

                    list.add(labelName);


                }

                if (StringUtils.isNotEmpty(label.getState())){

                    Predicate state = criteriaBuilder.equal(root.get("state"), label.getState());

                    list.add(state);


                }
                Predicate[] predicates = new Predicate[list.size()];
                list.toArray(predicates);
                Predicate predicate = criteriaBuilder.and(predicates);
                return predicate;


            }
        };

        return spec;

    }
}
